package com.cn.http.service.impl;

import cn.com.allunion.sms.pojo.Smsrecord;
import com.alibaba.fastjson.JSON;
import com.cn.http.service.AbstractSmsSendService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 企信通短信发送服务
 * Created by YangJie on 2016/4/21.
 */
public class QiXinTongSmsSendService extends AbstractSmsSendService {
    private static final Logger LOGGER = LoggerFactory.getLogger(QiXinTongSmsSendService.class) ;

    /**
     * 电话号码前缀
     */
    protected final String phoneNumberPrefix ;

    /**
     * 电话号码群发分隔符
     */
    protected final String phoneNumberGroupSeparator ;

    public QiXinTongSmsSendService(String phoneNumberPrefix, String phoneNumberGroupSeparator) {
        this.phoneNumberPrefix = phoneNumberPrefix ;
        this.phoneNumberGroupSeparator = phoneNumberGroupSeparator ;
    }

    @Override
    public String send(List<Smsrecord> smsrecords) {
//        LOGGER.debug("QiXinTong:item" + item);
//        LOGGER.debug("qixintong:sbi" + sbi.getSmsBusinessName());
        String result = null;

        if (smsrecords == null || smsrecords.size() == 0) {
            return "sms记录为空";
        }
        /**
         * 后加的群发功能，为了不影响单发，单独判断群发
         */
        // if (smsRecos.size() != 1) {
        // String smsOperator = "";
        // String operatorMessageID = "";
        // String sendContent = "";
        // String sendNumber = item;
        // LOGGER.debug("短信群发电话号码处理前:" + sendNumber);
        // LOGGER.debug("短信群发电话号码处理后为:"
        // + dealTels(HttpClients.QXTparam, sendNumber,
        // HttpClients.QXTsepar));
        // String sendState = "";
        // StringBuffer smsRecordID = new StringBuffer();
        // String businessRecord = "";
        // String sign = "";

        // for (SMSRecord smsRecord2 : smsRecos) {
        // String res = recordAction.addNewRecord(smsRecord2);
        // LOGGER.debug(smsRecord2.getSmsRecordID());
        // LOGGER.debug("res=" + res);
        // if (res.equals("success")) {
        // smsOperator = smsRecord2.getSmsOperator().getJoinNum();
        //
        // operatorMessageID = smsRecord2.getOperatorMessageID();
        // sendContent = smsRecord2.getSendContent();
        // sendState = smsRecord2.getSendState();
        // businessRecord = smsRecord2.getBusinessRecord()
        // .getBusinessCode();
        // sign = "【" + sbi.getBusinessSignature() + "】";
        // if (smsRecordID.length() == 0) {
        // LOGGER.debug("res=" + 1);
        // smsRecordID.append(smsRecord2.getSmsRecordID());
        // } else {
        // LOGGER.debug("res=" + 2);
        // smsRecordID.append(HttpClients.QXTsepar
        // + smsRecord2.getSmsRecordID());
        // }
        //
        // result = "接收成功！";
        // } else {
        // result = "发送之前存储失败";
        // LOGGER.debug("发送之前存储失败");
        // }
        // }
        // String mess = "{'operating':'null','smsOperator':" + "'"
        // + smsOperator
        // + "'"
        // + ",'operatorMessageID':"
        // + "'"
        // + operatorMessageID
        // + "'"
        // + ",'sendContent':"
        // + "'"
        // + sendContent
        // + "'"
        // + ",'sendNumber':"
        // + "'"
        // + dealTels(HttpClients.QXTparam, sendNumber,
        // HttpClients.QXTsepar) + "'" + ",'sendState':" + "'"
        // + sendState + "'" + ",'smsRecordID':" + "'"
        // + smsRecordID.toString() + "'" + ",'businessRecord':" + "'"
        // + businessRecord + "'" + ",'sign':" + sign + "}";
        // LOGGER.debug("群发短信前json为:" + mess);
        // sendSMS.send(mess);
        //
        // } else {
        /**
         * 单发短信功能
         */
        for (Smsrecord smsRecord2 : smsrecords) {
            String res = smsrecordService.addNewRecord(smsRecord2);
            String sendNumber = smsRecord2.getSendNumber();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("短信记录ID： {}", smsRecord2.getSmsRecordID());
                LOGGER.debug("短信单发电话号码处理前: {}", sendNumber);
            }
            // 处理电话号码
            sendNumber = dealTels(phoneNumberPrefix, sendNumber, phoneNumberGroupSeparator);
            smsRecord2.setSendNumber(sendNumber);

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("短信单发电话号码处理后为: {}", smsRecord2.getSendNumber());
            }
            if (res.equals("success")) {

//                String mess = "{'operating':'null','smsOperator':" + "'"
//                        + smsRecord2.getSmsOperator().getJoinNum() + "'"
//                        + ",'operatorMessageID':" + "'"
//                        + smsRecord2.getOperatorMessageID() + "'"
//                        + ",'sendContent':" + "'" + smsRecord2.getSendContent()
//                        + "'" + ",'sendNumber':" + "'" + sendNumber + "'"
//                        + ",'sendState':" + "'" + smsRecord2.getSendState()
//                        + "'" + ",'smsRecordID':" + "'"
//                        + smsRecord2.getSmsRecordID() + "'"
//                        + ",'businessRecord':" + "'"
//                        + smsRecord2.getSmsBusinessRecord().getBusinessCode()
//                        + "'" + ",'sign':" + "'【" + smsRecord2.getSmsBusinessRecord().getBusinessSignature()
//                        + "】'" + "}";


                // 发送消息到消息队列
                String message = getSendMessage(smsRecord2, smsRecord2.getSmsBusinessRecord().getBusinessSignature()) ;
                if(LOGGER.isDebugEnabled()) {
                    LOGGER.debug("message： {}", message);
                }

                // 发送消息到消息队列
                if (StringUtils.isNotEmpty(message)) {
                    sendSMS.send(message);
                    result = "接收成功！";
                } else {
                    result = "接收失败，无法获取转换后的消息， smsrecord: " + JSON.toJSONString(smsRecord2);
                }
            } else {
                result = "发送之前存储失败";
                LOGGER.debug("发送之前存储失败");
            }
        }

        // }
        return result;
    }

    /**
     * 电话号码处理方法
     * @param param 处理格式 比如加86
     * @param tels 待处理的电话号码
     * @param separetor 分隔符
     * @return 处理后的电话号码
     */
    public String dealTels(String param, String tels, String separetor) {
        tels = param + tels;
        tels = tels.replace(separetor, separetor + param);

        return tels;
    }

    public String getPhoneNumberPrefix() {
        return phoneNumberPrefix;
    }

    public String getPhoneNumberGroupSeparator() {
        return phoneNumberGroupSeparator;
    }
}
